﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OracleClient;

public partial class Services_getCorrelationCoefficient : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

        string AgriParamList = "";
        string RsIndexList = "";
        string ImageID = "";
        string DistrictId = "";
        string SamplingDate = "";
        //11-4日添加,查询数据库的表的名称---->
        string param_tbname = "";
        string rs_tbname = "";
        //<----11-4日添加

        //11-11日添加，计算的类型---->
        string modelType = "";
        //<----11-11日添加，选择的品质指标
        string resultXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
        resultXml += "<RxyList>" + "\n";

        ImageID = Request.QueryString[0].ToString().Trim();
        AgriParamList = Request.QueryString[1].ToString().Trim();
        RsIndexList = Request.QueryString[2].ToString().Trim();
        modelType = Request.QueryString[3].ToString().Trim();//11-12日添加
        //SamplingDate = ImageID.Remove(2);

        string queryString = "select DISTID,IMAGEDATE from RS.DISTRICT_IMAGE_SP_REF where IMAGEID = '" + ImageID + "'";
        OracleDataReader myReader = OracleDataBase.ExecuteReader(queryString);
        if (myReader.Read())
        {
            DistrictId = myReader[0].ToString();
            SamplingDate =Convert.ToDateTime( myReader[1]).ToShortDateString();
        }


        string[] AgriParamArray = AgriParamList.Split(',');
        //判断，以确定要查询的表名---->
        if (AgriParamArray[0].Trim() == "YEILD")
        {
            param_tbname = "rs.sp_yeild_param";
            rs_tbname = "rs.sp_rsindex";
        }
        else
        {
            param_tbname = "rs.sp_agri_param";
            rs_tbname = "rs.sp_rsindex";
        }
        //<----11-4日添加
        string[] RsIndexArray = RsIndexList.Split(',');
        int AgriParamCount = AgriParamArray.Length;
        int RsIndexCount = RsIndexArray.Length;
        try
        {
            if (modelType == "quality")//计算品质相关系数
            {
                for (int i = 0; i < AgriParamCount; i++)
                {
                    for (int j = 0; j < RsIndexCount; j++)
                    {
                        AgriRSModel myModel;
                        if (AgriParamArray[i].StartsWith("UP_"))
                        {
                            AgriParamArray[i] = AgriParamArray[i].Substring(3);
                            //myModel = new AgriRSModel(AgriParamArray[i], RsIndexArray[j], SamplingDate, DistrictId);
                            myModel = new AgriRSModel(AgriParamArray[i], RsIndexArray[j], SamplingDate, DistrictId, param_tbname,rs_tbname);
                        }
                        else
                        {
                            //myModel = new AgriRSModel(AgriParamArray[i], RsIndexArray[j], SamplingDate, DistrictId);
                            myModel = new AgriRSModel(AgriParamArray[i], RsIndexArray[j], SamplingDate, DistrictId, param_tbname,rs_tbname);
                            //myModel = new AgriRSModel(AgriParamArray[i], QualityArray[j], SamplingDate, DistrictId, param_tbname);
                        }


                        double R = myModel.calculateRelatedCoefficient(myModel.AgriParamDataset, myModel.RSIndexDataset);
                        resultXml += "<Rxy agri = '" + AgriParamArray[i] + "' quality = '" + RsIndexArray[j] + "' r = '" + R + "' />" + "\n";

                    }
                }

            }else if(modelType=="Rsquality")
            {
                for (int i = 0; i < AgriParamCount; i++)
                {
                    for (int j = 0; j < RsIndexCount; j++)
                    {
                        AgriRSModel myModel;
                        if (AgriParamArray[i].StartsWith("UP_"))
                        {
                            AgriParamArray[i] = AgriParamArray[i].Substring(3);
                            //myModel = new AgriRSModel(AgriParamArray[i], RsIndexArray[j], SamplingDate, DistrictId);
                            myModel = new AgriRSModel(AgriParamArray[i], RsIndexArray[j], SamplingDate, DistrictId, rs_tbname);
                        }
                        else
                        {
                            //myModel = new AgriRSModel(AgriParamArray[i], RsIndexArray[j], SamplingDate, DistrictId);
                            myModel = new AgriRSModel(AgriParamArray[i], RsIndexArray[j], SamplingDate, DistrictId, rs_tbname);
                            //myModel = new AgriRSModel(AgriParamArray[i], QualityArray[j], SamplingDate, DistrictId, param_tbname);
                        }


                        double R = myModel.calculateRelatedCoefficient(myModel.QualityDataset, myModel.RSIndexDataset);
                        resultXml += "<Rxy agri = '" + AgriParamArray[i] + "' quality = '" + RsIndexArray[j] + "' r = '" + R + "' />" + "\n";

                    }
                }
            }
            else
            {
                for (int i = 0; i < AgriParamCount; i++)
                {
                    for (int j = 0; j < RsIndexCount; j++)
                    {
                        AgriRSModel myModel;
                        if (AgriParamArray[i].StartsWith("UP_"))
                        {
                            AgriParamArray[i] = AgriParamArray[i].Substring(3);
                            //myModel = new AgriRSModel(AgriParamArray[i], RsIndexArray[j], SamplingDate, DistrictId);
                            myModel = new AgriRSModel(AgriParamArray[i], RsIndexArray[j], SamplingDate, DistrictId, param_tbname, rs_tbname);
                        }
                        else//待仔细处理
                        {
                            //myModel = new AgriRSModel(AgriParamArray[i], RsIndexArray[j], SamplingDate, DistrictId);
                            myModel = new AgriRSModel(AgriParamArray[i], RsIndexArray[j], SamplingDate, DistrictId, param_tbname, rs_tbname);
                        }


                        double R = myModel.calculateRelatedCoefficient(myModel.AgriParamDataset, myModel.RSIndexDataset);
                        resultXml += "<Rxy agri = '" + AgriParamArray[i] + "' rsindex = '" + RsIndexArray[j] + "' r = '" + R + "' />" + "\n";

                    }
                }
            }
            
            resultXml += "</RxyList>" + "\n";

            Response.Write(resultXml);
        }
        catch (Exception ex)
        {
            resultXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
            resultXml += "<RxyList>" + "\n";
            resultXml += "</RxyList>" + "\n";
            Response.Write(resultXml);
            throw ex;
        }
    }
}
